package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"pic-text-hub/models"
	"pic-text-hub/pkg"
)

func Register(c *gin.Context) {
	username := c.PostForm("username")
	password := pkg.Md5(c.PostForm("password"))
	data := models.User{
		Username: username,
		Password: password,
	}
	if data.ID > 0 {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "用户已存在",
		})
		return
	}
	err := models.DB.Debug().Create(&data).Error

	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "注册成功",
		})
		return
	} else {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 200,
			"msg":  "注册成功",
		})
		return
	}

}
